<?php
/**
 * Probably rewrite this class not to inherit from generic item
 *
 */
namespace Yz\modules\Uploads;
class Item_UploadedFile
    extends \Yz\modules\Generic\Item_Default
{

    /**
     *  @var Yz_Descriptor_Interface
     */
    protected static $descriptor;


    /**
     *  @var array
     */
    protected static $properties = '
        id: { type: id }
        filename: { type: string }
        originalname: { type: string }
        extension: { type: string }
        created: { type: date }
        filesize: { type: number }
    ';

    protected $uploadPath;

    /**
     *  @var Yz_File
     */
    protected $file;


    /**************************
     *      Public methods    *
     **************************/



    /**
     *
     *  @return string path, relative to webRoot, with trailing slash
     */
    public function getUploadPath()
    {
        // get path relative to YZ_ROOT
        $path =& $this->uploadPath;
        if (! $path) {
            $path = $this->model->getUploadPath();
        }
        return $path;
    }

    /**
     *
     *  @return string path, relative to webRoot, with trailing slash
     */
    public function getFileSize()
    {
        debug();
        if (! array_key_exists('filesize', $this->values)) {
            $this->values['filesize'] = null;
        }
        $fs =& $this->values['filesize'];
        if (null === $fs) {
            $file = $this->getFile();
            if (null !== $file) {
                $fs = $file->getSize();
            }
        }
        // get path relative to YZ_ROOT
        return $fs;
    }

    /**
     *  @return Yz_File
     */
    public function getFile()
    {
        debug();
        $file =& $this->file;
        if (null === $file) {
            $link = $this->getLinkToFile();
            if (null !== $link) {
                $file = new \Yz_File($link, YZ_ROOT);
            } else {
                $file = null;
            }
        }
        return $file;
    }

    /** get the link to file, relative to web root
     *
     *  @return string|null
     */
    public function getLinkToFile()
    {
        debug();
        $fn = $this->filename;
        if (null !== $fn) {
            $path = $this->getUploadPath();
            $link = $path.'/'.$fn;
        } else {
            $link = null;
        }
        return $link;
    }

    /** get the HTML code for displaying
     *
     *  @return string|null
     */
    public function buildHTML(\Yz_Template_Interface $t)
    {
        debug();
        $link = $this->getLinkToFile();
        if (null === $link) {
            return null;
        }
        $html = <<<html
            <a href="{$t->webRoot}{$link}">{$this->filename}</a>\n
html;
        return $html;
    }

    /**
     *
     *  @return string absolute path with trailing slash
     */
    public function getUploadPathAbsolute()
    {
        // get path relative to YZ_ROOT
        $path = $this->getUploadPath();
        $path = YZ_ROOT . trim($path, '/') . '/';
        if (! is_dir($path)) {
            throw new \Yz_Exception( 'The upload directory does not exist' );
        }
        return $path;
    }

} // class
